package cj.primariaiquest.integration.endpoints;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.Logger;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.stereotype.Component;

import cj.primariaiquest.model.Ticket;

/**
 * This class is a service activator. It simulates a desk.
 * 
 * @author parvu georgian
 * 
 */
@Component
public class AlimentationDesk {
    private static final String ALIMENTATION_DESK_NAME = "alimentation desk ";

    private static Logger logger = Logger.getLogger(AlimentationDesk.class);

    private Random random = new Random();
    private String deskName;
    public static AtomicInteger count = new AtomicInteger(1);

    public AlimentationDesk() {
	this.deskName = ALIMENTATION_DESK_NAME + (count.getAndAdd(1));
    }

    /**
     * Simulates the process time of a ticket. Also add's to ticket the desk
     * number where it was processed.
     * 
     * @param ticket
     *            Ticket to be processed
     * @return processed Ticket.
     */
    @ServiceActivator(inputChannel = "filteredAlimentationTicketsSubscribableChannel", outputChannel = "processedTicketsChannel")
    public Ticket processTicket(Ticket ticket) {
	try {
	    Thread.sleep(random.nextInt(10) * 500);
	    ticket.setDesk(deskName);
	} catch (InterruptedException e) {
	    e.printStackTrace();
	    logger.error(e.getMessage());
	}
	return ticket;
    }

}
